//
//  ST_TropyIntimacyHeaderView.swift
//  btvideo
//
//  Created by mac on 2021/7/9.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit

class ST_TropyIntimacyHeaderView: UICollectionReusableView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        [champion_view,runner_up_view,third_place_view].forEach({ addSubview($0) })
        
        champion_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(2)
            make.centerX.equalToSuperview()
            make.width.equalTo(102)
            make.bottom.equalToSuperview()
        }
        
        runner_up_view.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(7)
            make.left.equalToSuperview().offset(TIANCompatibleValue(value: 25))
            make.width.equalTo(92)
            make.bottom.equalToSuperview()
        }
        
        third_place_view.snp.makeConstraints { (make) in
            make.top.width.height.equalTo(runner_up_view)
            make.right.equalToSuperview().offset(TIANCompatibleValue(value: -25))
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    fileprivate lazy var champion_view:IntimacyWinnerView = {
        let control = IntimacyWinnerView(avatarSize: CGSize(width: 64, height: 64), avatarBorderColor: #colorLiteral(red: 1, green: 0.8352941176, blue: 0.2274509804, alpha: 1), hatImage: ST_ImageUtils.imageName("rank_intimacy_champion_icon"))
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var runner_up_view:IntimacyWinnerView = {
        let control = IntimacyWinnerView(avatarSize: CGSize(width: 54, height: 54), avatarBorderColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1), hatImage: ST_ImageUtils.imageName("rank_intimacy_runner_up_icon"))
        control.alpha = 0
        return control
    }()
    
    fileprivate lazy var third_place_view:IntimacyWinnerView = {
        let control = IntimacyWinnerView(avatarSize: CGSize(width: 54, height: 54), avatarBorderColor: #colorLiteral(red: 0.9098039216, green: 0.7058823529, blue: 0.4862745098, alpha: 1), hatImage: ST_ImageUtils.imageName("rank_intimacy_third_place_icon"))
        control.alpha = 0
        return control
    }()
    
}

extension ST_TropyIntimacyHeaderView{
    
    func bind(data_array:[ST_TrophyIntimacyCellData]) {
        
        champion_view.bind(data: data_array.first)
        runner_up_view.bind(data: data_array[safe:1])
        third_place_view.bind(data: data_array[safe:2])
        
    }
    
}


//MARK: - winner View
fileprivate class IntimacyWinnerView:UIView{
    
    convenience init(avatarSize:CGSize,avatarBorderColor:UIColor,hatImage:UIImage?){
        self.init(frame: .zero)
        
        hat_image_view.snp.makeConstraints { (make) in
            make.centerX.top.equalToSuperview()
            make.size.equalTo(CGSize(width: 44, height: 44))
        }
        hat_image_view.image = hatImage
        
        male_avatar_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(avatarSize)
            make.left.equalToSuperview()
            make.top.equalTo(hat_image_view.snp.bottom).offset(-4)
        }
        
        male_avatar_image_view.cornerRadius = avatarSize.width/2.0
        male_avatar_image_view.borderColor = avatarBorderColor
        male_avatar_image_view.borderWidth = 2
        
        female_avatar_image_view.snp.makeConstraints { (make) in
            make.size.equalTo(avatarSize)
            make.right.equalToSuperview()
            make.top.equalTo(hat_image_view.snp.bottom).offset(-4)
        }
        
        female_avatar_image_view.cornerRadius = avatarSize.width/2.0
        female_avatar_image_view.borderColor = avatarBorderColor
        female_avatar_image_view.borderWidth = 2
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        [female_avatar_image_view,male_avatar_image_view,hat_image_view,femaleNicknameLabel,male_nickname_label,male_sex_image_view,female_sex_image_view,content_label].forEach{ addSubview($0) }
        
        content_label.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview().offset(-20)
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
        }
        
        femaleNicknameLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
            make.width.lessThanOrEqualTo(80)
            make.bottom.equalToSuperview().offset(-35)
        }
        
        female_sex_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(femaleNicknameLabel.snp.right).offset(4)
            make.centerY.equalTo(femaleNicknameLabel)
        }
        
        
        
        male_nickname_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.height.equalTo(17)
            make.width.lessThanOrEqualTo(80)
            make.bottom.equalToSuperview().offset(-55)
        }
        
        male_sex_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(male_nickname_label.snp.right).offset(4)
            make.centerY.equalTo(male_nickname_label)
        }
        
        
    }
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var male_avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var female_avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var hat_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        return control
    }()
    
    fileprivate lazy var femaleNicknameLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.medium)
        return control
    }()
    
    
    fileprivate lazy var male_nickname_label:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = .white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.medium)
        return control
    }()
    
    fileprivate lazy var male_sex_image_view:UIImageView = {
        let control = UIImageView(image: ST_ImageUtils.imageName("TrophyIntimacyMale_white_icon"))
        return control
    }()
    
    fileprivate lazy var female_sex_image_view:UIImageView = {
        let control = UIImageView(image: ST_ImageUtils.imageName("TrophyIntimacyFemale_white_icon"))
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0.6047196062)
        control.font = UIFont.systemFont(ofSize: 12)
        return control
    }()
    
    weak var data:ST_TrophyIntimacyCellData?
    
    func bind(data:ST_TrophyIntimacyCellData?)  {
        
        self.data = data
        
        guard let item = data else {
            self.alpha = 0
            return
        }
        
        self.alpha = 1
        
        content_label.text = item.intimacy.string
        
        
        male_avatar_image_view.pin_setImage(from: item.manUser.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        male_nickname_label.text = item.manUser.nickname
        
        female_avatar_image_view.pin_setImage(from: item.womanUser.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        femaleNicknameLabel.text = item.womanUser.nickname
        
        
        
    }
    
    
    
    @objc fileprivate func click(gez:UITapGestureRecognizer){
        
        guard let d =  data else { return  }
        var accid:String
        if gez.view == male_avatar_image_view{
            accid = d.manUser.accid
            
            if d.manUser.rankClose == 2{
                self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
             return
            }
        }else{
            accid = d.womanUser.accid
            if d.womanUser.rankClose == 2{
                self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
             return
            }
        }
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
}

